package com.example.permission.service.impl;

import cn.hutool.core.convert.Convert;
import com.example.permission.common.CommonParam;
import com.example.permission.common.page.Page;
import com.example.permission.entity.dto.RoleMenuDTO;
import com.example.permission.entity.dto.SysRoleDTO;
import com.example.permission.entity.dto.page.SysRolePageDTO;
import com.example.permission.entity.po.SysAcl;
import com.example.permission.entity.po.SysRole;
import com.example.permission.enums.DataStatusEnum;
import com.example.permission.mapper.SysAclMapper;
import com.example.permission.mapper.SysRoleMapper;
import com.example.permission.service.SysRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 角色信息(SysRole)表服务实现类
 *
 * @author xuwq
 * @date 2020-05-09 15:56:07
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysAclMapper sysAclMapper;

    @Override
    public SysRole insert(SysRoleDTO sysRoleDTO) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleDTO, sysRole);
        sysRole.setCreateTime(new Date());
        sysRole.setCreator(CommonParam.getInstance().getUserNo());
        this.sysRoleMapper.insert(sysRole);
        return sysRole;
    }

    @Override
    public SysRole update(SysRoleDTO sysRoleDTO) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleDTO, sysRole);
        sysRole.setUpdateTime(new Date());
        sysRole.setUpdator(CommonParam.getInstance().getUserNo());
        this.sysRoleMapper.update(sysRole);
        return this.queryById(sysRole.getRoleId());
    }

    @Override
    public boolean deleteById(Integer roleId) {
        SysRole sysRole = new SysRole();
        sysRole.setRoleId(roleId);
        sysRole.setDataStatus(DataStatusEnum.ABNORMAL.getCode());
        return this.sysRoleMapper.update(sysRole) > 0;
    }

    @Override
    public SysRole queryById(Integer roleId) {
        return this.sysRoleMapper.queryById(roleId);
    }

    @Override
    public Page<SysRole> queryListByPage(SysRolePageDTO sysRolePageDTO) {
        int count = this.sysRoleMapper.queryCountByPage(sysRolePageDTO);
        List<SysRole> sysRoleList = this.sysRoleMapper.queryListByPage(sysRolePageDTO);
        return new Page<>(count, sysRoleList);
    }

    @Override
    public void roleMenuDistribution(Integer roleId, RoleMenuDTO roleMenuDTO) {
        sysAclMapper.deleteByRoleId(roleId);
        String menuIds = roleMenuDTO.getMenuIds();
        Arrays.stream(menuIds.split(",")).forEach(c -> {
            SysAcl sysAcl = new SysAcl();
            sysAcl.setRoleId(roleId);
            sysAcl.setRoleId(Convert.toInt(c));
            sysAclMapper.insert(sysAcl);
        });
    }

}